package com.fhz.shop.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.fhz.shop.R;
import com.fhz.shop.base.BaseFragment;
import com.fhz.shop.callback.ApiResponse;
import com.fhz.shop.databinding.FragmentHomeBinding;
import com.fhz.shop.entity.GoodsInfoLocal;
import com.fhz.shop.entity.PageHelper;
import com.fhz.shop.ui.details.DetailsActivity;
import com.fhz.shop.ui.search.SearchActivity;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.callback.Callback;
import com.hyphenate.helpdesk.easeui.util.IntentBuilder;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class HomeFragment extends BaseFragment<HomeViewModel,FragmentHomeBinding> {

    HomeRecyclerViewAdapter mHomeRecyclerViewAdapter;
    List<GoodsInfoLocal> mInfoLocalList;
    @Override
    protected void initData() {
        setRecyclerView();
        //刷新头和尾部
        setRefreshLayout();


        binding.homeSearchView.setOnClickListener(v -> {
            SearchActivity.launch(getActivity());
        });


    }

    private void setRecyclerView() {
        binding.homeRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        mInfoLocalList = new ArrayList<>();
        mHomeRecyclerViewAdapter = new HomeRecyclerViewAdapter(getContext(),mInfoLocalList);
        binding.homeRecyclerView.setAdapter(mHomeRecyclerViewAdapter);

        //请求数据,然后刷新
        viewModel.getGoodsInfo(1,10).observe(this, new Observer<ApiResponse<PageHelper<GoodsInfoLocal>>>() {
            @Override
            public void onChanged(ApiResponse<PageHelper<GoodsInfoLocal>> pageHelperApiResponse) {
                PageHelper<GoodsInfoLocal> data = pageHelperApiResponse.getData();
                List<GoodsInfoLocal> list = data.getList();
                mInfoLocalList.addAll(list);
                mHomeRecyclerViewAdapter.notifyDataSetChanged();
            }
        });


    }

    private void setRefreshLayout() {
//        binding.homeRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
//        binding.homeRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
        binding.homeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        binding.homeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });

        ChatClient.getInstance().login("wang", "123456", new Callback(){
            @Override
            public void onSuccess() {
                System.out.println("登录成功!");
            }

            @Override
            public void onError(int code, String error) {
                System.out.println(error);
            }

            @Override
            public void onProgress(int progress, String status) {
                System.out.println(status);
            }
        });

        mHomeRecyclerViewAdapter.setOnItemClickListener((v, position) -> {

            //添加到数据库.或者添加到购物车
            Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();

            //打开详情
            DetailsActivity.launch(getActivity(),mInfoLocalList.get(position).getId());

//            //
//            if(ChatClient.getInstance().isLoggedInBefore()){
//                //已经登录，可以直接进入会话界面
//                Intent intent = new IntentBuilder(getActivity())
//                        .setServiceIMNumber("kefuchannelimid_549605") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
//                        .build();
//                startActivity(intent);
//            }else{
//                //未登录，需要登录后，再进入会话界面
//                Toast.makeText(getActivity(), "先登录!", Toast.LENGTH_SHORT).show();
//            }

        });

    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected HomeViewModel createViewModel() {

        return new ViewModelProvider(this).get(HomeViewModel.class);
    }
}